home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue22 / using_m4.m4 < prev    next >
Encoding:
Text File  |  2002-08-14  |  25.0 KB  |  924 lines

  1. <!-- -*- html -*- -->
  2.  
  3. m4_include(stdlib.m4)
  4.  
  5. <!-- Copyright (C) 1997 Bob Hepple
  6.  
  7. This is an example of the use of m4 to pre-process HTML
  8. code. Using the FSF version of m4, you generate HTML from
  9. this file with:
  10.  
  11.     m4 -P using_m4.m4 >using_m4.html
  12.  
  13. Please don't be put off by the use of nested quotation marks
  14. in the code examples. This sample is rather an extreme
  15. stress-test of the idea. Normal usage is much simpler.
  16.  
  17. This program is free software; you can redistribute it
  18. and/or modify it under the terms of the GNU General Public
  19. License as published by the Free Software Foundation; either
  20. version 2 of the License, or (at your option) any later
  21. version.
  22.  
  23. This program is distributed in the hope that it will be
  24. useful, but WITHOUT ANY WARRANTY; without even the implied
  25. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  26. PURPOSE.  See the GNU General Public License for more
  27. details.
  28.  
  29. You should have received a copy of the GNU General Public
  30. License along with this program; if not, write to the Free
  31. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  32. 02139, USA. -->
  33.  
  34. _HEADER(Mechanical Web Authoring - using _EM(m4) to write HTML.)
  35. <P>
  36. _HEAD1(Contents:)
  37.  
  38. _Start_TOC
  39.  
  40. <P><HR><SMALL><CENTER>This page last updated on m4_esyscmd(date)<BR>
  41. $Revision: 1.1.1.1 $</CENTER></SMALL><HR><P>
  42. _H1(`Some limitations of HTML')
  43.  
  44. It's amazing how easy it is to write simple HTML pages - and
  45. the availability of _EM(WYSIWYG) HTML editors like
  46. _EM(NETSCAPE GOLD) lulls one into a mood of _EM(`"don''`t
  47. worry, be happy"'). However, managing multiple,
  48. interrelated pages of HTML rapidly gets very, very
  49. difficult.  I recently had a slightly complex set of pages
  50. to put together and it started me thinking - _EM(`"there has
  51. to be an easier way"').
  52.  
  53. <P>
  54.  
  55. I immediately turned to the WWW and looked up all sorts of
  56. tools - but quite honestly I was rather disappointed. Mostly,
  57. they were what I would call _EM(Typing Aids) - instead of
  58. having to remember arcane incantations like _CODE(<a
  59. href="link">text</a>), you are given a button or a
  60. magic keychord like ALT-CTRL-j which remembers the syntax and
  61. does all that nasty typing for you.  
  62.  
  63. <P>
  64.  
  65. _EM(Linux) to the rescue! HTML is built as ordinary text
  66. files and therefore the normal _EM(Linux) text management
  67. tools can be used. This includes the revision control tools
  68. such as _EM(RCS) and the text manipulation tools like
  69. _EM(`awk, perl, etc.') These offer significant help in
  70. version control and managing development by multiple users
  71. as well as in automating the process of extracting from a
  72. database and displaying the results (the classic _CODE(`"grep
  73. |sort |awk"') pipeline).
  74.  
  75. <P>
  76.  
  77. The use of these tools with HTML is documented elsewhere,
  78. e.g. see Jim Weinrich's article in _EM(Linux Journal) Issue
  79. 36, April 1997, "Using Perl to Check Web Links" which I'd
  80. highly recommend as yet another way to really flex those
  81. _EM(Linux) muscles when writing HTML.
  82.  
  83. <P>
  84.  
  85. What I will cover here is a little work I've done recently
  86. with using _EM(m4) in maintaining HTML. The ideas can
  87. probably be extended to the more general SGML case very
  88. easily.
  89.  
  90. <P>
  91. _LINK_TO_LABEL(Contents)
  92. <P>
  93. _H1(`Using _EM(m4)')
  94.  
  95. I decided to use _EM(m4) after looking at various other
  96. pre-processors including _EM(cpp), the _EM(C)
  97. front-end. While _EM(cpp) is perhaps a little too
  98. _EM(C)-specific to be very useful with HTML, _EM(m4) is a
  99. very generic and clean macro expansion program - and it's
  100. available under most Unices including _EM(Linux).
  101.  
  102. <P>
  103.  
  104. Instead of editing _EM(*.html) files, I create
  105. _EM(*.m4) files with my favourite text editor.
  106. These look something like this:
  107.  
  108. <P>
  109. _CODEQUOTE(``m4_include(stdlib.m4)
  110. _HEADER(`This is my header')
  111. <P>This is some plain text<P>
  112. _HEAD1(`This is a main heading')
  113. <P>This is some more plain text<P>
  114. _TRAILER'')
  115.  
  116. <P>
  117.  
  118. The `format' is simple - just HTML code but you can now
  119. `include' files and add macros rather like in _EM(C). I use
  120. a convention that my new macros are in capitals and start
  121. with "_" to make them stand out from HTML language and to
  122. avoid name-space collisions.
  123.  
  124. <P>
  125.  
  126. The _EM(m4) file is then processed as follows to create an
  127. _EM(.html) file e.g.
  128.  
  129. <P>
  130. _CODEQUOTE(m4 -P <file.m4 >file.html)
  131. <P>
  132.  
  133. This is especially easy if you create a "makefile" to
  134. automate this in the usual way. Something like:
  135.  
  136. <P>
  137. _CODEQUOTE(.SUFFIXES: .m4 .html
  138. .m4.html:
  139.     m4 -P $*.m4 >$*.html
  140. default: index.html
  141. *.html: stdlib.m4
  142. all: default PROJECT1 PROJECT2
  143. PROJECT1:
  144.     (cd project2; make all)
  145. PROJECT2:
  146.     (cd project2; make all))
  147. <P>
  148.  
  149. The most useful commands in _EM(m4) include the following
  150. which are very similar to the _EM(cpp) equivalents (shown in
  151. brackets):
  152.  
  153. <DL>
  154. <DT>_CODE(``m4_include''):
  155. <DD>includes a common file into your HTML (_CODE(`#include'))
  156. <DT>_CODE(``m4_define''):
  157. <DD>defines an _EM(m4) variable (_CODE(`#define'))
  158. <DT>_CODE(``m4_ifdef''):
  159. <DD>a conditional (_CODE(`#ifdef'))
  160. </DL>
  161.  
  162. <P>
  163.  
  164. Some other commands which are useful are:
  165.  
  166. <DL>
  167. <DT>_CODE(``m4_changecom''):
  168. <DD>change the _EM(m4) comment character (normally #)
  169. <DT>_CODE(``m4_debugmode''):
  170. <DD>control error disgnostics
  171. <DT>_CODE(``m4_traceon/off''):
  172. <DD>turn tracing on and off
  173. <DT>_CODE(``m4_dnl''):
  174. <DD>comment
  175. <DT>_CODE(``m4_incr, m4_decr''):
  176. <DD>simple arithmetic
  177. <DT>_CODE(``m4_eval''):
  178. <DD>more general arithmetic
  179. <DT>_CODE(``m4_esyscmd''):
  180. <DD>execute a _EM(Linux) command and use the output
  181. <DT>_CODE(``m4_divert(i)''):
  182.  
  183. <DD>This is a little complicated, so skip on first reading. It is a
  184. way of storing text for output at the end of normal processing - it
  185. will come in useful later, when we get to automatic numbering of
  186. headings. It sends output from _EM(m4) to a temporary file number
  187. _EM(i). At the end of processing, any text which was diverted is then
  188. output, in the order of the file number _EM(i). File number -1 is the
  189. bit bucket and can be used to comment out chunks of comments. File
  190. number 0 is the normal output stream. Thus, for example, you can
  191. _CODE(``m4_divert'') text to file 1 and it will only be output at the end.
  192.  
  193. </DL>
  194.  
  195. <P>
  196.  
  197. _LINK_TO_LABEL(Contents)
  198. <P>
  199.  
  200. _H1(`Examples of _EM(m4) macros')
  201. _H2(`Sharing HTML elements across several page')
  202. <P>
  203.  
  204. In many "nests" of HTML pages, each page shares elements
  205. such as a button bar like this:
  206.  
  207. <P>
  208. <BLOCKQUOTE>_LINK(nil,[Home]) _LINK(nil,[Next]) _LINK(nil,[Prev]) _LINK(nil,[Index])</BLOCKQUOTE>
  209. <P>
  210.  
  211. This is fairly easy to create in each page - the trouble is
  212. that if you make a change in the "standard" button-bar then
  213. you then have the tedious job of finding each occurance of
  214. it in every file and then manually make the changes. 
  215.  
  216. <P>
  217.  
  218. With _EM(m4) we can more easily do this by putting the
  219. shared elements into an _CODE(m4_include) statement, just like
  220. _EM(C).
  221.  
  222. <P>
  223.  
  224. While I'm at it, I might as well also automate the naming of
  225. pages, perhaps by putting the following into an `include'
  226. file, say _CODE("button_bar.m4"):
  227.  
  228. <P>
  229. _CODEQUOTE(``m4_define(`_BUTTON_BAR', 
  230.     <a href="homepage.html">[Home]</a>
  231.     <a href="$1">[Next]</a>
  232.     <a href="$2">[Prev]</a>
  233.     <a href="indexpage.html">[Index]</a>)'')
  234. <P>
  235.  
  236. and then in the document itself:
  237.  
  238. <P>
  239. _CODEQUOTE(``m4_include button_bar.m4
  240. _BUTTON_BAR(`page_after_this.html', 
  241.     `page_before_this.html')'')
  242.  
  243. <P>
  244.  
  245. The $1 and $2 parameters in the macro definition are
  246. replaced by the strings in the macro call.
  247.  
  248. <P>
  249. _LINK_TO_LABEL(Contents)
  250.  
  251. <P>
  252.  
  253. _H2(`Managing HTML elements that often change')
  254.  
  255. <P>
  256.  
  257. It is very troublesome to have items change in multiple HTML
  258. pages. For example, if your email address changes then you
  259. will need to change all references to the new
  260. address. Instead, with _EM(m4) you can do something like
  261. this in your _CODE(stdlib.m4) file:
  262.  
  263. <P>
  264. _CODEQUOTE(``m4_define(`_EMAIL_ADDRESS', `MyName@foo.bar.com')'')
  265. <P>
  266.  
  267. and then just put _CODE(``_EMAIL_ADDRESS'') in your
  268. _EM(m4) files. 
  269.  
  270. <P>
  271.  
  272. A more substantial example comes from building strings up
  273. with multiple components, any of which may change as the
  274. page is developed. If, like me, you develop on one machine,
  275. test out the page and then upload to another machine with a
  276. totally different address then you could use the
  277. _CODE(m4_ifdef) command in your _CODE(stdlib.m4) file (just
  278. like the _CODE(#ifdef) command in _EM(cpp)):
  279.  
  280. <P>
  281.  
  282. _CODEQUOTE(``m4_define(`_LOCAL')
  283. .
  284. .
  285. m4_define(`_HOMEPAGE', 
  286.     m4_ifdef(`_LOCAL', `//127.0.0.1/~YourAccount', 
  287.         `http://ISP.com/~YourAccount'))
  288.  
  289. m4_define(`_PLUG', `<A REF="http://www.ssc.com/linux/">
  290.     <IMG SRC="_HOMEPAGE/gif/powered.gif" 
  291.     ALT="[Linux Information]"> </A>')'')
  292.  
  293. <P>
  294.  
  295. Note the careful use of quotes to prevent the variable
  296. _CODE(``_LOCAL'') from being expanded. _CODE(``_HOMEPAGE'')
  297. takes on different values according to whether the variable
  298. _CODE(``_LOCAL'') is defined or not. This can then ripple
  299. through the entire project as you _EM(make) the pages.
  300.  
  301. <P>
  302.  
  303. In this example, _CODE(``_PLUG'') is a macro to advertise
  304. _EM(Linux).  When you are testing your pages, you use the
  305. local version of _CODE(``_HOMEPAGE''). When you are ready to
  306. upload, you can remove or comment out the _CODE(``_LOCAL'')
  307. definition like this:
  308.  
  309. <P>
  310.  
  311. _CODEQUOTE(``m4_dnl m4_define(`_LOCAL')'')
  312.  
  313. <P>
  314.  
  315. ... and then _EM(re-make).
  316.  
  317. <P>
  318. _LINK_TO_LABEL(Contents)
  319.  
  320. <P>
  321. _H2(`Creating new text styles')
  322.  
  323. Styles built into HTML include things like _CODE(<EM>) for emphasis and _CODE(<CITE>) for citations. With _EM(m4) you can define your own, new styles like this:
  324.  
  325. <P>
  326. _CODEQUOTE(``m4_define(`_MYQUOTE',
  327.     <BLOCKQUOTE><EM>$1</EM></BLOCKQUOTE>)'')
  328. <P>
  329.  
  330. If, later, you decide you prefer _CODE(<STRONG>) instead
  331. of _CODE(<EM>) it is a simple matter to change the
  332. definition and then every _CODE(_MYQUOTE) paragraph falls
  333. into line with a quick _CODE(make).
  334.  
  335. <P>
  336.  
  337. The classic guides to good HTML writing say things like "It
  338. is strongly recommended that you employ the logical styles
  339. such as _CODE(<EM>...</EM>) rather than the physical
  340. styles such as _CODE(<I>...</I>) in your documents."
  341. Curiously, the _EM(WYSIWYG) editors for HTML generate purely
  342. physical styles. Using these _EM(m4) styles may be a good
  343. way to keep on using logical styles.
  344.  
  345. <P>
  346. _LINK_TO_LABEL(Contents)
  347. <P>
  348.  
  349. _H2(`Typing and mnemonic aids')
  350. <P>
  351.  
  352. I don't depend on _EM(WYSIWYG) editing (having been brought
  353. up on _EM(troff)) but all the same I'm not averse to using
  354. help where it's available. There is a choice (and maybe it's
  355. a fine line) to be made between:
  356.  
  357. <P>
  358. _CODEQUOTE(<BLOCKQUOTE><PRE><CODE>Some code you want to display.
  359. </CODE></PRE></BLOCKQUOTE>)
  360. <P>
  361. and:
  362. <P>
  363.  
  364. _CODEQUOTE(``_CODE(Some code you want to display.)'')
  365. <P>
  366.  
  367. In this case, you would `define' _CODE(``_CODE'') like this:
  368.  
  369. <P>
  370. _CODEQUOTE(``m4_define(`_CODE',
  371.      <BLOCKQUOTE><PRE><CODE>$1</CODE></PRE></BLOCKQUOTE>)'')<P>
  372.  
  373. Which version you prefer is a matter of taste and convenience
  374. although the _EM(m4) macro certainly saves some typing and
  375. ensures that HTML codes are not interleaved. Another example
  376. I like to use (I can never remember the syntax for links) is:
  377.  
  378. <P>
  379. _CODEQUOTE(``m4_define(`_LINK', <a href="$1">$2</a>)'')
  380.  
  381. <P>
  382.  
  383. Then, <P>
  384.  
  385. _CODE(<a href="URL_TO_SOMEWHERE">Click here to get to SOMEWHERE </a>) <P>
  386.  
  387. becomes: <P>
  388.  
  389. _CODE(``_LINK(`URL_TO_SOMEWHERE', `Click here to get to SOMEWHERE')'')
  390.  
  391. <P>
  392. _LINK_TO_LABEL(Contents)
  393. <P>
  394.  
  395. _H2(`Automatic numbering')
  396. <P>
  397.  
  398. _EM(m4) has a simple arithmetic facility with two operators
  399. _CODE(m4_incr) and _CODE(m4_decr) which act as you might
  400. expect - this can be used to create automatic numbering,
  401. perhaps for headings, e.g.:
  402.  
  403. _CODEQUOTE(``m4_define(_CARDINAL,0)
  404.  
  405. m4_define(_H, `m4_define(`_CARDINAL',
  406.     m4_incr(_CARDINAL))<H2>_CARDINAL.0 $1</H2>')
  407.  
  408. _H(First Heading)
  409. _H(Second Heading)'')
  410.  
  411. <P>
  412.  
  413. This produces:
  414.  
  415. _CODEQUOTE(``<H2>1.0 First Heading</H2>
  416. <H2>2.0 Second Heading</H2>'')
  417.  
  418. <P>
  419. _LINK_TO_LABEL(Contents)
  420. <P>
  421.  
  422. _H2(`Automatic date stamping')
  423.  
  424. For simple, datestamping of HTML pages I use the
  425. _CODE(`m4_esyscmd') command to maintain an automatic
  426. timestamp on every page:
  427.  
  428. <P>
  429. _CODEQUOTE(``This page was updated on m4_esyscmd(date)'')
  430. <P>
  431.  
  432. which produces:
  433.  
  434. <P>
  435. This page was last updated on Fri May  9 10:35:03 HKT 1997
  436. <P>
  437.  
  438. Of course, you could also use the date, revision and other
  439. facilities of revision control systems like _EM(RCS) or
  440. _EM(SCCS), e.g. _CODE(`$Da'`te$').
  441.  
  442. <P>
  443. _LINK_TO_LABEL(Contents)
  444. <P>
  445.  
  446. _H2(`Generating Tables of Contents')
  447. <P>
  448.  
  449. Using _EM(m4) allows you to `define' commonly repeated
  450. phrases and use them consistently - I hate repeating myself
  451. because I am lazy and because I make mistakes, so I find
  452. this feature absolutely key.
  453.  
  454. <P>
  455.  
  456. A good example of the power of _EM(m4) is in building a
  457. table of contents in a big page (like this one). This
  458. involves repeating the heading title in the table of
  459. contents and then in the text itself. This is tedious and
  460. error-prone especially when you change the titles. There are
  461. specialised tools for generating tables of contents from
  462. HTML pages but the simple facility provided by _EM(m4) is
  463. irresistable to me. 
  464.  
  465. <P>
  466. _H3(`Simple to understand TOC')
  467. <P>
  468.  
  469. The following example is a fairly simple-minded Table of
  470. Contents generator. First, create some useful macros in
  471. _CODE(stdlib.m4):
  472.  
  473. <P>
  474. _CODEQUOTE(``m4_define(`_LINK_TO_LABEL', <A HREF="#$1">$1</A>)
  475. m4_define(`_SECTION_HEADER', <A NAME="$1"><H2>$1</H2></A>)'')
  476. <P>
  477.  
  478. Then `define' all the section headings in a table at the
  479. start of the page body:
  480.  
  481. <P>
  482. _CODEQUOTE(``m4_define(`_DIFFICULTIES', `The difficulties of HTML')
  483. m4_define(`_USING_M4', `Using <EM>m4</EM>')
  484. m4_define(`_SHARING', `Sharing HTML Elements Across Several Pages')'')
  485. <P>
  486. Then build the table:
  487.  
  488. <P>
  489. _CODEQUOTE(``<UL><P>
  490.     <LI> _LINK_TO_LABEL(_DIFFICULTIES)
  491.     <LI> _LINK_TO_LABEL(_USING_M4)
  492.     <LI> _LINK_TO_LABEL(_SHARING)
  493. <UL>'')
  494. <P>
  495. Finally, write the text:
  496. <P>
  497. _CODEQUOTE(``.
  498. .
  499. _SECTION_HEADER(_DIFFICULTIES)
  500. .
  501. .'')
  502. <P>
  503.  
  504. The advantages of this approach are that if you change
  505. your headings you only need to change them in one place
  506. and the table of contents is automatically regenerated;
  507. also the links are guaranteed to work.
  508.  
  509. <P>
  510.  
  511. Hopefully, that simple version was fairly easy to understand.
  512.  
  513. <P>
  514. _LINK_TO_LABEL(Contents)
  515.  
  516. <P>
  517. _H3(`Simple to use TOC')
  518. <P>
  519.  
  520. The Table of Contents generator that I normally use is a bit
  521. more complex and will require a little more study, but is
  522. much easier to use. It not only builds the Table, but it
  523. also automatically numbers the headings on the fly - up to 4
  524. levels of numbering (e.g. section 3.2.1.3 - although this
  525. can be easily extended). It is very simple to use as
  526. follows:
  527.  
  528. <P>
  529. <OL>
  530. <LI>Where you want the table to appear, call _CODE(``Start_TOC'')
  531. <LI>at every heading use _CODE(``_H1(`Heading for level 1')'') or _CODE(``_H2(`Heading for level 2')'') as appropriate.
  532. <LI>After the very last HTML code (probably after </HTML>), call _CODE(``End_TOC'')
  533. <LI> and that's all!
  534. </OL>
  535. <P>
  536.  
  537. The code for these macros is a little complex, so hold your breath:
  538.  
  539. _CODEQUOTE(``m4_define(_Start_TOC,`<UL><P>m4_divert(-1)
  540.   m4_define(`_H1_num',0)
  541.   m4_define(`_H2_num',0)
  542.   m4_define(`_H3_num',0)
  543.   m4_define(`_H4_num',0)
  544.   m4_divert(1)')
  545.  
  546. m4_define(_H1, `m4_divert(-1)
  547.   m4_define(`_H1_num',m4_incr(_H1_num))
  548.   m4_define(`_H2_num',0)
  549.   m4_define(`_H3_num',0)
  550.   m4_define(`_H4_num',0)
  551.   m4_define(`_TOC_label',`_H1_num. $1')
  552.   m4_divert(0)<LI><A HREF="#_TOC_label">_TOC_label</A>
  553.   m4_divert(1)<A NAME="_TOC_label">
  554.     <H2>_TOC_label</H2></A>')
  555. .
  556. .
  557. [definitions for _H2, _H3 and _H4 are similar and are 
  558. in the downloadable version of stdlib.m4]
  559. .
  560. .
  561.  
  562. m4_define(_End_TOC,`m4_divert(0)</UL><P>')'')
  563.  
  564. <P>
  565.  
  566. This works by using the _CODE(``m4_divert(1)'') command in
  567. _CODE(``_Start_TOC'') to send all the remaining text from the file to
  568. an (internal) temporary file - _EM(m4) just calls it "file 1".
  569.  
  570. <P>
  571.  
  572. From then on, whenever an _CODE(``_H1'') or _CODE(``_H2'') etc command
  573. is reached, the relevant header numbering variables are incremented
  574. (with _CODE(``m4_incr'')) and the Table of Contents entry is sent to
  575. the standard output (diverted to file 0) together with automatically
  576. generated pointers into the main text.
  577.  
  578. <P>
  579.  
  580. The diversion to file 1 is then resumed for the regular text and an
  581. automatically generated section heading with pointer target is added.
  582.  
  583. <P>
  584.  
  585. The _CODE(``_End_TOC'') statement _EM(must) be placed at the end of
  586. the file. When it is reached the text which was diverted to file 1 is
  587. read back to standard output.
  588.  
  589. <P>
  590.  
  591. The net result is that the Table of Contents appears near the start of
  592. the final file, with automatically generated pointers to the correct
  593. section in the later text.
  594.  
  595. <P>
  596.  
  597. Of course, if you plan on using the _CODE(``m4_divert'') command in your own
  598. text, you will have to check that it does not clash with the Table of
  599. Contents generator.
  600.  
  601. <P>
  602. _LINK_TO_LABEL(Contents)
  603. <P>
  604.  
  605. _H2(`Simple tables')
  606. <P>
  607.  
  608. Other than Tables of Contents, many browsers support tabular
  609. information. Here are some funky macros as a short cut to
  610. producing these tables. First, an example of their use:
  611.  
  612. <P>
  613. _CODEQUOTE(``<CENTER>
  614. _Start_Table(BORDER=5)
  615. _Table_Hdr(,Apples, Oranges, Lemons)
  616. _Table_Row(England,100,250,300)
  617. _Table_Row(France,200,500,100)
  618. _Table_Row(Germany,500,50,90)
  619. _Table_Row(Spain,,23,2444)
  620. _Table_Row(Denmark,,,20)
  621. _End_Table
  622. </CENTER>'')
  623. <P>
  624.  
  625. <CENTER>
  626. _Start_Table(BORDER=5)
  627. _Table_Hdr(,Apples, Oranges, Lemons)
  628. _Table_Row(England,100,250,300)
  629. _Table_Row(France,200,500,100)
  630. _Table_Row(Germany,500,50,90)
  631. _Table_Row(Spain,,23,2444)
  632. _Table_Row(Denmark,,,20)
  633. _End_Table
  634. </CENTER><P>
  635.  
  636. ...and now the code. Note that this example utilises _EM(m4's)
  637. ability to recurse:
  638.  
  639.  
  640. _CODEQUOTE(``m4_dnl _Start_Table(Columns,TABLE parameters)
  641. m4_dnl defaults are BORDER=1 CELLPADDING="1" CELLSPACING="1"
  642. m4_dnl WIDTH="n" pixels or "n%" of screen width
  643. m4_define(_Start_Table,`<TABLE $1>')
  644.  
  645. m4_define(`_Table_Hdr_Item', `<th>$1</th>
  646.   m4_ifelse($#,1,,`_Table_Hdr_Item(m4_shift($@))')')
  647.  
  648. m4_define(`_Table_Row_Item', `<td>$1</td>
  649.   m4_ifelse($#,1,,`_Table_Row_Item(m4_shift($@))')')
  650.  
  651. m4_define(`_Table_Hdr',`<tr>_Table_Hdr_Item($@)</tr>')
  652. m4_define(`_Table_Row',`<tr>_Table_Row_Item($@)</tr>')
  653.  
  654. m4_define(`_End_Table',</TABLE>)'')
  655.  
  656.  
  657. <P>
  658. _LINK_TO_LABEL(Contents)
  659. <P>
  660.  
  661. _H1(`_EM(m4) gotchas')
  662. <P>
  663.  
  664. Unfortunately, _EM(m4) is not unremitting sweetness and
  665. light - it needs some taming and a little time spent on
  666. familiarisation will pay dividends. Definitive documentation
  667. is available (for example in _EM(emacs' info) documentation
  668. system) but, without being a complete tutorial, here are a
  669. few tips based on my fiddling about with the thing.
  670.  
  671. <P>
  672. m4_define(_GOTCHA,0)
  673. m4_define(`_GOTCHA', m4_incr(_GOTCHA))
  674. _H2(`Gotcha _GOTCHA - quotes')
  675. <P>
  676.  
  677. m4_changequote([,])_EM(m4's) quotation characters are the
  678. _EM(grave) accent ` which starts the quote, and the
  679. _EM(acute) accent ' m4_changequote(`,')which ends it. It may
  680. help to put all arguments to macros in quotes, e.g.
  681.  
  682. <P>
  683. _CODEQUOTE(``_HEAD1(`This is a heading')'')
  684. <P>
  685.  
  686. The main reason for this is in case there are commas in an
  687. argument to a macro - _EM(m4) uses commas to separate macro
  688. parameters, e.g. _CODE(``_CODE(foo, bar)'') would print the
  689. _CODE(foo) but not the _CODE(bar). _CODE(``_CODE(`foo,
  690. bar')'') works properly.
  691.  
  692. <P>
  693.  
  694. This becomes a little complicated when you nest macro
  695. calls as in the _EM(m4) source code for the examples in this
  696. paper - but that is rather an extreme case and normally you
  697. would not have to stoop to that level.
  698.  
  699. <P>
  700. _LINK_TO_LABEL(Contents)
  701. <P>
  702.  
  703. m4_define(`_GOTCHA', m4_incr(_GOTCHA))
  704. _H2(`Gotcha _GOTCHA - Word swallowing')
  705. <P>
  706.  
  707. The worst problem with _EM(m4) is that some versions of
  708. it "swallow" key words that it recognises, such as
  709. "include", "format", "divert", "file", "gnu", "line",
  710. "regexp", "shift", "unix", "builtin" and "define". You
  711. can protect these words by putting them in _EM(m4) quotes,
  712. for example:
  713.  
  714. <P>
  715.  
  716. _CODEQUOTE(``Smart people `include' Linux in their list
  717. of computer essentials.'')
  718.  
  719. <P>
  720.  
  721. The trouble is, this is a royal pain to do - and you're
  722. likely to forget which words need protecting.  
  723.  
  724. <P>
  725.  
  726. Another, safer way to protect keywords (my preference) is to
  727. invoke _EM(m4) with the _CODE(-P) or
  728. _CODE(--prefix-builtins) option. Then, all builtin macro
  729. names are modified so they all start with the prefix
  730. _CODE(m4_) and ordinary words are left alone.  For example,
  731. using this option, one should write _CODE(m4_define) instead
  732. of _CODE(``define'') (as shown in the examples in this
  733. article). 
  734.  
  735. <P>
  736.  
  737. The only trouble is that not all versions of
  738. _EM(m4) support this option - notably some PC versions under
  739. M$-DOS. Maybe that's just another reason to steer clear of
  740. hack code on M$-DOS and stay with _EM(Linux!)
  741.  
  742. <P>
  743. _LINK_TO_LABEL(Contents)
  744. <P>
  745.  
  746. m4_define(`_GOTCHA', m4_incr(_GOTCHA))
  747. _H2(`Gotcha _GOTCHA - Comments')
  748. <P>
  749.  
  750. Comments in _EM(m4) are introduced with the `#' character -
  751. everything from the `#' to the end of the line is ignored by
  752. _EM(m4) and simply passed unchanged to the output. If you
  753. want to use `#' in the HTML page then you would need to quote it
  754. like this - ``#''. Another option (my preference) is to
  755. change the _EM(m4) comment character to something exotic
  756. like this: _CODE(``m4_changecom(`[[[[')'') and not have to
  757. worry about ``#'' symbols in your text.
  758.  
  759. <P>
  760.  
  761. If you want to use comments in the _EM(m4) file which do not
  762. appear in the final HTML file, then the macro
  763. _CODE(``m4_dnl'') (dnl = _STRONG(D)elete to _STRONG(N)ew _STRONG(L)ine) is for you. This suppresses everything
  764. until the next newline. 
  765. <P>
  766. _CODEQUOTE(``m4_define(_NEWMACRO, `foo bar') m4_dnl This is a comment'')
  767.  
  768. <P>
  769.  
  770. Yet another way to have source code ignored is the
  771. _CODE(``m4_divert'') command. The main purpose of
  772. _CODE(``m4_divert'') is to save text in a temporary buffer for
  773. inclusion in the file later on - for example, in building a
  774. table of contents or index. However, if you divert to "-1"
  775. it just goes to limbo-land. This is useful for getting rid
  776. of the whitespace generated by the _CODE(``m4_define'')
  777. command, e.g.:
  778.  
  779. <P>
  780. _CODEQUOTE(``m4_divert(-1) diversion on
  781. m4_define(this ...)
  782. m4_define(that ...)
  783. m4_divert    diversion turned off'')
  784.  
  785. <P>
  786. _LINK_TO_LABEL(Contents)
  787. <P>
  788.  
  789. m4_define(`_GOTCHA', m4_incr(_GOTCHA))
  790. _H2(`Gotcha _GOTCHA - Debugging')
  791. <P>
  792.  
  793. Another tip for when things go wrong is to increase the
  794. amount of error diagnostics that _EM(m4) emits. The
  795. easiest way to do this is to add the following to your
  796. _EM(m4) file as debugging commands:
  797.  
  798. <P>
  799. _CODEQUOTE(``m4_debugmode(e)
  800. m4_traceon
  801. .
  802. .
  803. buggy lines
  804. .
  805. .
  806. m4_traceoff'')
  807. <P>
  808.  
  809. <P>
  810. _LINK_TO_LABEL(Contents)
  811. <P>
  812.  
  813. _H1(`Conclusion')
  814.  
  815. "ah ha!", I hear you say. "HTML 3.0 already has an include
  816. statement". Yes it has, and it looks like this:
  817.  
  818. <P>
  819. _CODEQUOTE(``<!--#include file="junk.html" -->'')
  820. <P>
  821.  
  822. The problem is that:
  823. <UL>
  824.  
  825.     <LI> The work of including and interpreting the
  826. include is done on the server-side before downloading and
  827. adds a big overhead as the server has to scan files for
  828. ``include'' statements.
  829.  
  830.     <LI> Consequently most servers (especially public ISP's) deactivate this feature.
  831.  
  832.     <LI> ``include'' is all you get - no macro
  833. substitution, no parameters to macros, no ifdef, etc, etc.
  834.  
  835. </UL>
  836.  
  837. <P>
  838.  
  839. There are several other features of _EM(m4) that I have not
  840. yet exploited in my HTML ramblings so far, such as regular
  841. expressions and doubtless many others. It might be
  842. interesting to create a "standard" _CODE(stdlib.m4) for
  843. general use with nice macros for general text processing and
  844. HTML functions. By all means download my version of
  845. _CODE(stdlib.m4) as a base for your own hacking. I would be
  846. interested in hearing of useful macros and if there is
  847. enough interest, maybe a Mini-HOWTO could evolve from this
  848. paper.
  849.  
  850. <P>
  851.  
  852. There are many additional advantages in using _EM(Linux) to
  853. develop HTML pages, far beyond the simple assistance given
  854. by the typical _EM(Typing Aids) and _EM(WYSIWYG) tools.
  855.  
  856. <P>
  857.  
  858. Certainly, this little hacker will go on using _EM(m4) until
  859. HTML catches up - I will then do my last _EM(make) and drop
  860. back to using pure HTML.
  861.  
  862. <P>
  863.  
  864. I hope you enjoy these little tricks and encourage you to
  865. contribute your own. Happy hacking!
  866.  
  867. <P>
  868. _H1(`Files to download')
  869.  
  870. You can get the HTML and the _EM(m4) source code for this
  871. article here (for the sake of completeness, they're
  872. copylefted under GPL 2):
  873.  
  874. <P>
  875. _PRE(_FTP(using_m4.html, using_m4.html)    :this file
  876. _FTP(using_m4.m4, using_m4.m4)    :_EM(m4) source
  877. _FTP(stdlib.m4, stdlib.m4)    :Include file
  878. _FTP(makefile, makefile))
  879.  
  880. <P>
  881. _LINK_TO_LABEL(Contents)
  882. <P>
  883. _H1(`Bob Hepple')
  884.  
  885. <P>
  886.  
  887. _EMAILME(Bob Hepple) has been hacking at Unix since 1981
  888. under a variety of excuses and has somehow been paid for it
  889. at least some of the time. It's allowed him to pursue
  890. another interest - living in warm, exotic countries
  891. including Hong Kong, Australia, Qatar, Saudi Arabia, Lesotho
  892. and Singapore. His initial aversion to the cold
  893. was learned in the UK. Ambition - to stop working for the
  894. credit card company and taxman and to get a real job - doing
  895. this, of course!
  896.  
  897. <P>
  898. _LINK_TO_LABEL(Contents)
  899. <P>
  900.  
  901. _COUNTER(5, `since March 18th 1998')
  902.  
  903. <HR>
  904. This page last updated on m4_esyscmd(date)
  905. <P>
  906. <ADDRESS>
  907. _CREDITS
  908. _PLUG
  909. </ADDRESS>
  910. </BODY>
  911. </HTML>
  912. _End_TOC
  913.  
  914. m4_divert(-1)
  915. <!-- For emacs: -->
  916.  
  917. <!-- Local Variables: -->
  918. <!-- fill-column: 70 -->
  919. <!-- eval:(setq filename (substring buffer-file-name (string-match "[a-zA-Z0-9_.]+$" buffer-file-name))) -->
  920. <!-- eval:(setq basename (substring filename 0 (string-match "\\." filename))) -->
  921. <!-- eval:(setq compile-command (concat "make -k " basename ".html")) -->
  922. <!-- eval:(setq hm--html-automatic-changed-comment nil) -->
  923. <!-- eval:(setq hm--html-automatic-created-comment nil) -->
  924. <!-- End: -->